package com.hula.onion.book.server.spider.platforms.koudailicai;

import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.model.AccountInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by work on 2018/1/28.
 */
@Component
public class KouDaiLiCaiSpiderHelper {
    private static final Logger logger = LoggerFactory.getLogger(KouDaiLiCaiSpiderHelper.class);

    public Map<String, Object> installRequestParams(LoginForm loginForm) {
        Map<String, Object> map = new HashMap<>(4);
        map.put("username", loginForm.getUserName());
        map.put("password", loginForm.getPassword());
        map.put("imgcode", "");
        map.put("redirect_url", "https://www.koudailc.com/site/index");
        return map;
    }

    /**
     * 解析账户余额
     *
     * @param json
     * @return
     */
    public CommonResponse parseAccountBalance(JSONObject json) {
        if (json == null) {
            return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
        }
        try {
            AccountInfo accountInfo = new AccountInfo();
            JSONObject accountJson = json.getJSONObject("account");
            BigDecimal amount = accountJson.getBigDecimal("total_money");
            BigDecimal totalIntrest = accountJson.getBigDecimal("total_profits");
            BigDecimal yesterdayIntrest = accountJson.getBigDecimal("lastday_profits");
            accountInfo.setAmount(amount);
            accountInfo.setTotalIntrest(totalIntrest);
            accountInfo.setYesterdayIntrest(yesterdayIntrest);
            return new CommonResponse(accountInfo);
        } catch (Exception e) {
            logger.error("[XinYongBaoSpiderHelper] parseAccountBalance fail!", e);
        }
        return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
    }
}
